<?php
// +----------------------------------------------------------------------
// | ProjectName : domall
// +----------------------------------------------------------------------
// | Description :  基础类
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.idowe.com All rights reserved.
// +----------------------------------------------------------------------
// | Authors : Johhny <chenjf@idowe.com>  Date : 2016-02-03
// +----------------------------------------------------------------------
namespace app\admin\controller;


use think\Controller;
use think\View;

class Base extends Controller{
    /**
     * 管理员资料 name id group
     */
    protected $admin_info;

    /**
     * 权限内容
     */
    protected $permission;

    /**
     * 菜单
     */
    protected $menu;

    /**
     * 常用菜单
     */
    protected $quick_link;

    /**
     * 构造函数
     * Base constructor.
     */
    public function __construct() {
        parent::__construct();

        /**
         * 验证用户是否登录
         * $admin_info 管理员资料 name id
         */
        $this->admin_info = $this->systemLogin();
        $this->assign('admin_info',$this->admin_info);
    }

    /**
     * 系统后台登录验证，并判断是否已经登录，
     * 登录后则自动跳转平台后台首页
     * @param
     * @return array 数组类型的返回结果
     */
    protected final function systemLogin() {
        $user = unserialize(session('sys_key'));
        if ((empty($user['name']) || empty($user['id']))){
            if(CONTROLLER_NAME != 'login'){
                $this->redirect('/admin/login');
            }
        }else {
            if(CONTROLLER_NAME == 'login'){
                $this->redirect('/admin/index');
            }else{
                $this->systemSetKey($user);
            }
        }
        return $user;
    }

    /**
     * 重写fetch 方便管理视图 主题风格
     * @author Johhny <chenjf@idowe.com>
     * @param string $template
     * @param array $vars
     * @param array $cache
     * @param bool $renderContent
     * @return string
     * @throws \think\Exception
     */
    public function fetch($template = '', $vars = [], $cache = [], $renderContent = false){
//        $view = new View();
//        $view->assign($this->view);
        $this->view->theme(C('admin_theme'));
        $this->view->config('view_suffix',C('admin_view_suffix'));
        $this->view->config('view_depr',C('admin_view_depr'));
        return $this->view->fetch($template, $vars, $cache, $renderContent);
    }

    /**
     * 系统后台 会员登录后 将会员验证内容写入对应cookie中
     *
     * @param string $name 用户名
     * @param int $id 用户ID
     * @return bool 布尔类型的返回结果
     */
    protected final function systemSetKey($user, $avatar = '', $avatar_compel = false) {
        session('sys_key',serialize($user));
        if ($avatar_compel || $avatar != '') {
            session('admin_avatar',$avatar);
        }
    }

    /**
     * 取得IP
     *
     *
     * @return string 字符串类型的返回结果
     */
    function getIp(){
        $ip = $_SERVER['REMOTE_ADDR'];
        return preg_match('/^\d[\d.]+\d$/', $ip) ? $ip : '';
    }

}